package com.lzq.course.system.controller.admin;

import com.lzq.course.server.dto.RoleDto;
import com.lzq.course.server.dto.PageDto;
import com.lzq.course.server.dto.ResponseDto;
import com.lzq.course.server.service.RoleService;
import com.lzq.course.server.util.ValidatorUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class RoleController {

    public static final String BUSINESS_NAME = "角色";

    @Resource
    private RoleService roleService;

    /**
     * 查询列表 分页查询
     * @param pageDto
     * @return
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto<RoleDto> pageDto) {
        ResponseDto responseDto = new ResponseDto();
        roleService.list(pageDto);

        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 保存
     * @param roleDto
     * @return
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody RoleDto roleDto) {

        //保存校验
        ValidatorUtil.require(roleDto.getName(), "角色");
        ValidatorUtil.length(roleDto.getName(),"角色", 1, 50);
        ValidatorUtil.require(roleDto.getDesc(), "描述");
        ValidatorUtil.length(roleDto.getDesc(),"描述", 1, 100);

        ResponseDto responseDto = new ResponseDto();
        roleService.save(roleDto);

        responseDto.setContent(roleDto);
        return responseDto;
    }

    /**
     * 为角色分配权限
     * @param roleDto
     * @return
     */
    @PostMapping("/save-resource")
    public ResponseDto saveResource(@RequestBody RoleDto roleDto) {
        ResponseDto responseDto = new ResponseDto();
        roleService.saveResource(roleDto);

        responseDto.setContent(roleDto);
        return responseDto;
    }

    /**
     * 为用户分片角色
     * @param roleDto
     * @return
     */
    @PostMapping("/save-user")
    public ResponseDto saveUser(@RequestBody RoleDto roleDto) {
        ResponseDto responseDto = new ResponseDto();
        roleService.saveUser(roleDto);

        responseDto.setContent(roleDto);
        return responseDto;
    }

    /**
     * 获取角色权限
     * @param roleId 角色id
     * @return
     */
    @PostMapping("/list-resource/{roleId}")
    public ResponseDto listRoleResource(@PathVariable String roleId) {
        ResponseDto responseDto = new ResponseDto();
        List<String> resourceIds = roleService.listResource(roleId);
        responseDto.setContent(resourceIds);
        return responseDto;
    }

    /**
     * 获取角色关联的用户
     * @param roleId 角色id
     * @return
     */
    @GetMapping("/list-user/{roleId}")
    public ResponseDto listRoleUser(@PathVariable String roleId) {
        ResponseDto responseDto = new ResponseDto();
        List<String> userIds = roleService.listUser(roleId);
        responseDto.setContent(userIds);
        return responseDto;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        roleService.delete(id);

        return responseDto;
    }
}
